<?php
class ExperiencesController extends AppController {

	var $name = 'Experiences';

	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('index', 'view');
	}

	function index() {		
		$this->Experience->recursive = 0;
		$this->layout = 'home';
		
		$conditions = array();
		
		//filter city id
		$cities = $this->Experience->City->find('all');
		//$city_id = $this->Session->read("districts_city_id") ? $this->Session->read("districts_city_id"): "";
		if(!empty($this->params['form']['filter_city_id']))
			$city_id = $this->params['form']['filter_city_id'];
		else
			$city_id = $this->Session->read("Experience_city_id");
		
		$this->Session->write("Experience_city_id", $city_id);
		
		if($city_id && $city_id != "all")
		{
			$conditions["Experience.city_id"] = $city_id;			
		}
		
		//echo $this->Session->read("districts_city_id");
		
		//filter district id
		$districts = $this->Experience->District->find('all', array("conditions"=>array("District.city_id"=>$city_id)));		
		if(!empty($this->params['form']['filter_district_id']))
			$district_id = $this->params['form']['filter_district_id'];
		else
			$district_id = $this->Session->read("Experience_district_id");
		
		$this->Session->write("Experience_district_id", $district_id);
		
		if($district_id && $district_id != "all")
		{
			$conditions["Experience.district_id"] = $district_id;			
		}
		//echo $this->Session->read("districts_city_id");
		
		//filter career id
		$careers = $this->Experience->Career->find('all');		
		if(!empty($this->params['form']['filter_career_id']))
			$career_id = $this->params['form']['filter_career_id'];
		else
			$career_id = $this->Session->read("Experience_career_id");
		
		$this->Session->write("Experience_career_id", $career_id);
		
		if($career_id && $career_id != "all")
		{
			$conditions["Experience.career_id"] = $career_id;			
		}
		//echo $this->Session->read("districts_city_id");
		
		//filter keyword
		$keyword = $this->Session->read("Experience_keyword");
		if(isset($this->params['form']['filter_keyword']) && $this->params['form']['filter_keyword'] != '')
		{
			$keyword = $this->params['form']['filter_keyword'];
			$this->Session->write("Experience_keyword", $keyword);
		}
		if(isset($this->params['form']['filter_keyword']) && $this->params['form']['filter_keyword'] == '')
		{
			$keyword = '';
			$this->Session->write("Experience_keyword", $keyword);
		}
		
		
		
		if($keyword && $keyword != "")
		{
			$conditions["Experience.name LIKE ?"] = '%'.$keyword.'%';
			//$conditions["Product.name LIKE ?"] = '%'.$keyword.'%';
		}
		
		//var_dump($this->params);
		if(count($conditions))
		{
			$this->paginate = array(
				'conditions' => $conditions				
			);
		}
		
		$Experiences = $this->paginate();
		foreach($Experiences as $k => $item)
		{
			//$Experiences[$k]["Experience"]["price"] = $item["Experience"]["price"] != '' ? number_format($item["Experience"]["price"],0,",", "."): '';
			$Experiences[$k]["Experience"]["create_date"] = date('d/m/Y', strtotime($item["Experience"]["create_date"]));
		}
		
		$this->set('Experiences', $Experiences);
		$this->set('cities', $cities);
		$this->set('city_id', $city_id);
		$this->set('districts', $districts);		
		$this->set('district_id', $district_id);
		$this->set('careers', $careers);		
		$this->set('career_id', $career_id);
		$this->set('keyword', $keyword);	
	}
	
	function view($id = null) {
		$this->layout = 'home';
		if (!$id) {
			$this->Session->setFlash(__('Invalid Experience', true));
			$this->redirect(array('action' => 'index'));
		}
		$Experience = $this->Experience->read(null, $id);
		//$Experience["Experience"]["price"] = $Experience["Experience"]["price"] != '' ? number_format($Experience["Experience"]["price"],0,",", "."): '';
		$this->set('Experience', $Experience);
		
		
		$this->Experience->recursive = 0;
		
		$conditions = array();
		
		//filter city id
		$cities = $this->Experience->City->find('all');
		$city_id = $Experience["Experience"]["city_id"];		
		
		if($city_id && $city_id != "all")
		{
			$conditions["Experience.city_id"] = $city_id;			
		}
		
		//filter district id
		$districts = $this->Experience->District->find('all', array("conditions"=>array("District.city_id"=>$city_id)));		
		$district_id = $Experience["Experience"]["district_id"];
		
		if($district_id && $district_id != "all")
		{
			$conditions["Experience.district_id"] = $district_id;			
		}
		//echo $this->Session->read("districts_city_id");
		
		//filter career id
		$careers = $this->Experience->Career->find('all');		
		$career_id = $Experience["Experience"]["career_id"];
		
		if($career_id && $career_id != "all")
		{
			$conditions["Experience.career_id"] = $career_id;			
		}		

		if(count($conditions))
		{
			$this->paginate = array(
				'conditions' => $conditions				
			);
		}
		
		$this->set('Experiences', $this->paginate());
		$this->set('cities', $cities);
		$this->set('city_id', $city_id);
		$this->set('districts', $districts);		
		$this->set('district_id', $district_id);
		$this->set('careers', $careers);		
		$this->set('career_id', $career_id);
		$this->set('keyword', '');
	}
	
	function admin_index()
	{
		$this->Experience->recursive = 0;
		
		$conditions = array();
		
		//filter city id
		$cities = $this->Experience->City->find('all');
		//$city_id = $this->Session->read("districts_city_id") ? $this->Session->read("districts_city_id"): "";
		if(!empty($this->params['form']['filter_city_id']))
			$city_id = $this->params['form']['filter_city_id'];
		else
			$city_id = $this->Session->read("Experience_city_id");
		
		$this->Session->write("Experience_city_id", $city_id);
		
		if($city_id && $city_id != "all")
		{
			$conditions["Experience.city_id"] = $city_id;			
		}
		
		//echo $this->Session->read("districts_city_id");
		
		//filter district id
		$districts = $this->Experience->District->find('all', array("conditions"=>array("District.city_id"=>$city_id)));		
		if(!empty($this->params['form']['filter_district_id']))
			$district_id = $this->params['form']['filter_district_id'];
		else
			$district_id = $this->Session->read("Experience_district_id");
		
		$this->Session->write("Experience_district_id", $district_id);
		
		if($district_id && $district_id != "all")
		{
			$conditions["Experience.district_id"] = $district_id;			
		}
		//echo $this->Session->read("districts_city_id");
		
		//filter career id
		$careers = $this->Experience->Career->find('all');		
		if(!empty($this->params['form']['filter_career_id']))
			$career_id = $this->params['form']['filter_career_id'];
		else
			$career_id = $this->Session->read("Experience_career_id");
		
		$this->Session->write("Experience_career_id", $career_id);
		
		if($career_id && $career_id != "all")
		{
			$conditions["Experience.career_id"] = $career_id;			
		}
		
		////filter keyword		
		//if(!empty($this->params['form']['filter_keyword']))
		//	$keyword = $this->params['form']['filter_keyword'];
		//else
		//	$keyword = $this->Session->read("Experience_keyword");
		//
		//$this->Session->write("Experience_keyword", $keyword);
		//
		//if($keyword && $keyword != "")
		//{
		//	$conditions["Experience.name LIKE ?"] = '%'.$keyword.'%';
		//	//$conditions["Product.name LIKE ?"] = '%'.$keyword.'%';
		//}
		//
		//var_dump($this->params);
		if(count($conditions))
		{
			$this->paginate = array(
				'conditions' => $conditions				
			);
		}
		
		$this->set('Experiences', $this->paginate());
		$this->set('cities', $cities);
		$this->set('city_id', $city_id);
		$this->set('districts', $districts);		
		$this->set('district_id', $district_id);
		$this->set('careers', $careers);		
		$this->set('career_id', $career_id);
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Experience', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('Experience', $this->Experience->read(null, $id));
	}

	function admin_add() {
		$city_id = 1;
		$usr = $this->Auth->user();
		if (!empty($this->data)) {
			$this->Experience->create();
			if ($this->Experience->save($this->data)) {
				$this->Session->setFlash(__('The Experience has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Experience could not be saved. Please, try again.', true));
			}
			$city_id = $this->data["Experience"]["city_id"];
		}
		$this->data["Experience"]["user_id"] = $usr["User"]["id"];
		$users = $this->Experience->User->find('list');
		$cities = $this->Experience->City->find('list');
		$dits = $this->Experience->District->find('list', array("conditions"=>array("city_id"=>$city_id)));
		$districts[0] = "- ".__('Quận/Huyện', true)." -";
		foreach($dits as $key => $value)
		{
			$districts[$key] = $value;
		}
		$cas = $this->Experience->Career->find('list');
		$careers[0] = "- ".__('Ngành nghề', true)." -";
		foreach($cas as $key => $value)
		{
			$careers[$key] = $value;
		}
		$this->set(compact('users', 'cities', 'districts', 'careers'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Experience', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Experience->save($this->data)) {
				$this->Session->setFlash(__('The Experience has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Experience could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Experience->read(null, $id);
		}
		$users = $this->Experience->User->find('list');
		$cities = $this->Experience->City->find('list');
		$dits = $this->Experience->District->find('list', array("conditions"=>array("city_id"=>$this->data["Experience"]["city_id"])));
		$districts[0] = "- ".__('Quận/Huyện', true)." -";
		foreach($dits as $key => $value)
		{
			$districts[$key] = $value;
		}
		$cas = $this->Experience->Career->find('list');
		$careers[0] = "- ".__('Ngành nghề', true)." -";
		foreach($cas as $key => $value)
		{
			$careers[$key] = $value;
		}
		$this->set(compact('users', 'cities', 'districts', 'careers'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Experience', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Experience->delete($id)) {
			$this->Session->setFlash(__('Experience deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Experience was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
